<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/6/3
 * Time: 14:59
 */

namespace App\Setting\LazyCommon;

use App\Model\LazyCommon\RoleModel;
use App\Model\LazyCommon\UserGroupsModel;

class UserGroupsRoleSetting{

    /*手动获取的配置项*/
    public $entityCnName = "用户分组与角色";//模块名称
    public $table = "User_Groups_Role";//数据表名称
    public $editKeys = array();//后台管理列表中可以直接编辑的字段
    public $perPage = 100;//后台管理列表中可以直接编辑的字段

    /*自动获取的配置项*/
    public $bundleName;
    public $entityName;
    public $SettingPath;
    public $entityPath;
    public $entity;
    public $modelPath;
    public $em;

    public function __construct($em)
    {
        $this->bundleName = "App";
        $this->entityName = str_replace("\\","",str_replace("Setting","",str_replace(__NAMESPACE__,"",__CLASS__)));
        $this->SettingPath = __CLASS__;
        $this->entityPath = str_replace($this->entityName."Entity",$this->entityName,str_replace("Setting","Entity",str_replace("\\LazyCommon","",__CLASS__)));
        $this->entity = new $this->entityPath();
        $this->modelPath = str_replace("Setting","Model",__CLASS__);
        $this->em = $em;
    }

    public function listConfig()
    {
        $default = array();
        $default["User_Groups_Role AS UserGroupsRole"]["select"] = array("id","groups","role");
        $default["User_Groups_Role AS UserGroupsRole"]["search"] = array();
        $default["User_Groups_Role AS UserGroupsRole"]["condition"] = array("groups","role");
        $default["User_Groups_Role AS UserGroupsRole"]["orderBy"] = array("id","groups");

        $join = array();
        $join["User_Groups AS UserGroups"]["on"] = "UserGroupsRole.groups = UserGroups.id";
        $join["User_Groups AS UserGroups"]["select"] = array("name");
        $join["User_Groups AS UserGroups"]["search"] = array("name");
        $join["User_Groups AS UserGroups"]["condition"] = array();
        $join["User_Groups AS UserGroups"]["orderBy"] = array();

        $join["Role"]["on"] = "UserGroupsRole.role = Role.id";
        $join["Role"]["select"] = array("name","code");
        $join["Role"]["search"] = array("name");
        $join["Role"]["condition"] = array();
        $join["Role"]["orderBy"] = array();

        return array(
            "default"   => $default,
            "joins"     => $join,
        );
    }

    public function formConfig()
    {
        $UserGroupsModel = new UserGroupsModel($this->em);
        $UserGroups = $UserGroupsModel->getAllUserGroupsTree();
        $parents = array();
        foreach ($UserGroups as $k => $v) {
            $parents[$v["id"]] = $v["name"];
        }
        $options["groups"]["other"]["choices"] = array_flip($parents);
        $config["groups"] = array(
            "label"     =>"用户分组",
            "showType"  =>"select",
            "dataType"  =>"required",
            "option"    => $options["groups"]
        );

        $RoleModel = new RoleModel($this->em);
        $Role = $RoleModel->getAllRoleTree();
        $parents = array();
//        $parents[0] = "请选择角色";
        foreach ($Role as $k => $v) {
            $parents[$v["id"]] = $v["name"];
        }
        $options["role"]["other"] = array(
            "choices"   => array_flip($parents),
        );
        $config["role"] = array(
            "label"     =>"用户角色",
            "showType"  =>"checkbox",
            "dataType"  =>"required",
            "option"    => $options["role"]
        );

        return $config;
    }
}